package com.mrd.gtimp.base.service;

import com.mrd.base.BaseBiz;
import com.mrd.exception.QueryException;
import com.mrd.gtimp.base.entity.SystemValue;
import com.mrd.spring.repository.BaseRepository;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author qiul
 */
@Service
public class SystemValueBiz extends BaseBiz<SystemValue> {

    @Resource
    // 该处指定注入BaseRepository的dao对象
    @Qualifier("systemValueDao")
    public void setBaseRepository(BaseRepository<SystemValue, String> baseRepository) {
        super.baseRepository = baseRepository;
    }

    //查询申报 第二计量单位 sql
    private String findsql() {
        String sql = "select t.unit_code,t.unit_name from cdams_qp_unit t";
        return sql;
    }

    /**
     * 查询申报 第二计量单位 (下拉框)
     */
    public String[][] findUnitsOfMeasurementby(Pageable pageable) throws QueryException {
        List<Object> objects = findObjectBySql(findsql(), pageable.getPageNumber(), pageable.getPageSize(), null);
        return result(objects);
    }

    /**
     * 查询申报 第一计量单位
     */
    public String[][] findUnits1(String id) throws QueryException {
        Map<String, Object> map = new HashedMap();
        map.put("unit_code", id);
        String sql = "select t.unit_code,t.unit_name from cdams_qp_unit t where t.unit_code=:unit_code";
        List<Object> objects = findObjectBySql(sql, 0, 1, map);
        return result(objects);
    }

    /**
     * 查询申报 第二计量单位 (下拉框 2.0)
     */
    public String[][] findUnitsOfMeasurement() throws QueryException {
        List<Object> objects = findObjectBySql(findsql(), 0, 0, null);
        return result(objects);
    }

    //查询申报计量单位 返回结果
    private String[][] result(List<Object> objects) {
        String[][] hd_unit = new String[objects.size()][2];
        for (int i = 0; i < objects.size(); i++) {
            Object[] o = (Object[]) objects.get(i);
            hd_unit[i][0] = (String) o[1];
            hd_unit[i][1] = (String) o[0];
        }
        return hd_unit;
    }


    public String findChnsByIdentifiers(String identifiers) throws Exception {
        String result = "";
        String listSql = "select B_CHN_NAME from T_SYS_BASE ";
        String countSql = "select count(*) from T_SYS_BASE ";
        String whereCase = "where B_IDENTIFIER in (" + identifiers + ")";
        Map<String, Object> parameters = new HashMap<String, Object>();
        List<Object> countResult = findObjectBySql(countSql + whereCase, 1, 1, parameters);
        int size = ((BigDecimal) countResult.get(0)).intValue();
        List<Object> objResult = findObjectBySql(listSql + whereCase, 1, size, parameters);

        for (Object obj : objResult) {
            result += obj + ",";
        }
        if (!"".equals(result)) {
            result = result.substring(0, result.length() - 1);
        }

        return result;
    }


    public String findChnByIdentifier(String identifier) throws Exception {
        String result = "";

        String listSql = "select B_CHN_NAME from T_SYS_BASE ";
        // String countSql = "select count(*) from T_SYS_BASE ";
        String whereCase = "where B_IDENTIFIER =  :identifier ";
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("identifier", identifier);
        List<Object> objResult = findObjectBySql(listSql + whereCase, 1, 1, parameters);

        for (Object obj : objResult) {
            result = obj == null ? "" : obj.toString();
        }

        return result;
    }


    public String findChnsByIdentifiers(String[] identifiers) throws Exception {
        String identifiersStr = "";
        for (String identifier : identifiers) {
            identifiersStr += "'" + identifier + "',";
        }
        if ("".equals(identifiersStr)) {
            return "";
        } else {
            identifiersStr = identifiersStr.substring(0, identifiersStr.length() - 1);
        }
        return findChnsByIdentifiers(identifiersStr);
    }

}
